import pygame
from pygame import Surface
from timer import Timer

from settings import *

class Cookmenu:
    def __init__(self, player, cook_menu):
        self.player = player
        self.cook_menu = cook_menu
        self.display_surface = pygame.display.get_surface()
        self.font = pygame.font.Font('../font/LycheeSoda.ttf', 30)

        self.width = 400
        self.space = 10
        self.padding = 8

        self.cook_options = list(self.player.food_inventory.keys())
        self.foodstuff = list(self.player.food_inventory.values())
        self.food_border = len(self.player.food_inventory)-1
        self.setup()

        self.index = 3
        self.timer = Timer(200)
    def display_food(self):
        text_surf1 = self.font.render(f'{self.player.food}',False,'Black')
        text_rect = text_surf1.get_rect(midbottom = (SCREEN_WIDTH / 2,SCREEN_HEIGHT - 20))

        pygame.draw.rect(self.display_surface,'White',text_rect.inflate(10,10),0,4)
        self.display_surface.blit(text_surf1,text_rect)
    def setup(self):
        self.text_surfs = []
        self.total_height = 0
        self.foodstuff_surfs = []
        for item in self.cook_options:
            text_surf = self.font.render(item, False, 'Black')
            self.text_surfs.append(text_surf)
            self.total_height += text_surf.get_height() + (self.padding * 2)
        for it in self.foodstuff:
            foodstuff_surf = self.font.render(it, False, 'Black')
            self.foodstuff_surfs.append(foodstuff_surf)
        self.total_height += (len(self.text_surfs) - 1) * self.space
        self.menu_top = SCREEN_HEIGHT / 2 - self.total_height / 2
        self.main_rect = pygame.Rect(SCREEN_WIDTH / 2 - self.width / 2, self.menu_top, self.width, self.total_height)

        self.cook_text = self.font.render('cook',False,'Black')
        self.notenough_text = self.font.render('Foodstuff is not enough !',False,'red')
    def input(self):
        keys = pygame.key.get_pressed()
        self.timer.update()
        if keys[pygame.K_ESCAPE]:
            self.cook_menu()

        if not self.timer.active:

            if keys[pygame.K_UP]:
                self.index -=1
                self.timer.activate()
            if keys[pygame.K_DOWN]:
                self.index +=1
                self.timer.activate()

            if keys[pygame.K_SPACE]:
                self.timer.activate()
                current_food = self.cook_options[self.index]
                current_foodstuff = self.player.food_inventory[current_food]
                if self.player.item_inventory[current_foodstuff] > 0:
                    self.player.item_inventory[current_foodstuff] -=1
                    print(self.player.item_inventory[current_foodstuff])
                    self.player.food[current_food] +=1
                else:
                    self.display_surface.blit(self.notenough_text, (500,200))
                    self.timer = Timer(500)

    def show_entry(self, text_surf, amount, top,foodstuff_surf,selected):
            bg_rect = pygame.Rect(self.main_rect.left,top,self.width,text_surf.get_height() + (self.padding * 2))
            pygame.draw.rect(self.display_surface, 'White',bg_rect,0,4)

            text_rect1 = text_surf.get_rect(midleft = (self.main_rect.left + 20,bg_rect.centery))
            self.display_surface.blit(text_surf, text_rect1)
            foodstuff_rect = foodstuff_surf.get_rect(midright = (self.main_rect.right - 20, bg_rect.centery))
            self.display_surface.blit(foodstuff_surf, foodstuff_rect)

            if selected:
                pygame.draw.rect(self.display_surface,'black',bg_rect,4,4)
                pos_rect = self.cook_text.get_rect(midleft = (self.main_rect.left + 180,bg_rect.centery))
                self.display_surface.blit(self.cook_text,pos_rect)
    def update(self):
        self.input()
        self.display_food()
        x = 0
        for text_index, text_surf in enumerate(self.text_surfs):
            top = self.main_rect.top + text_index * (text_surf.get_height() + (self.padding * 2) + self.space)
            self.show_entry(text_surf, 0, top,self.foodstuff_surfs[x],self.index == text_index)
            x = x + 1
